--[[
Name: BrownieHelper
Developed by: Sonora (The Dragonflight)
Website: http://sonora.kirintor.googlepages.com/contents
SVN: http://code.google.com/p/browniehelper/source/browse
License: GNU General Public License v3
]]

--[[
~Configuration system events~
BrownieHelperConfig_PointPoolsChanged --> calls handler()
BrownieHelperConfig_EventsChanged --> calls handler()

]]

function BrownieHelper:initializeConfigurationPanels()

    --Initialize the panels
    ConfigureBrownieHelper:Initialize(self)
    ConfigurePointPools:Initialize(self)
    ConfigureMinimumBids:Initialize(self)
    ConfigureEvents:Initialize(self)
    ConfigureEventRewards:Initialize(self)
    ConfigureLootCategories:Initialize(self)
    ConfigurePermissions:Initialize(self)
    
end

--Main configuration page
function ConfigureBrownieHelper:Initialize(app)

    --save the app
    self.app = app
    
    --Setup our panelness, and inject us into the UI
    self.name = "BrownieHelper"
    self.okay = function() self:commitState() end
    self.cancel = function() self:clearState() end
    InterfaceOptions_AddCategory(self)
    
    ConfigureBrownieHelperBiddingInstructions:SetAutoFocus(false)
    ConfigureBrownieHelperRaidStartTime:SetAutoFocus(false)
    ConfigureBrownieHelperTimeAdjustment:SetAutoFocus(false)
    
    --setup control values
    self:clearState()
    
end

function ConfigureBrownieHelper:updateView()

    ConfigureBrownieHelperShowItemWindow:SetChecked(self.localOptions.ShowItemWindow)
    ConfigureBrownieHelperShowBidWindow:SetChecked(self.localOptions.ShowBidWindow)
    ConfigureBrownieHelperCloseAuctionDelay:SetValue(self.localOptions.CloseAuctionDelay)
    ConfigureBrownieHelperBiddingInstructions:SetText(self.localOptions.BiddingInstructions)
    ConfigureBrownieHelperRaidStartTime:SetText(self.localOptions.DefaultRaidStartTime)
    ConfigureBrownieHelperTimeAdjustment:SetText(self.localOptions.TimeAdjustmentPeriod)
    ConfigureBrownieHelperRecordRaidStart:SetChecked(self.localOptions.AutoRecordRaidStart)
    ConfigureBrownieHelperRecordTimeAdjustment:SetChecked(self.localOptions.AutoRecordTimeAdjustment)
    ConfigureBrownieHelperRecordBossEvents:SetChecked(self.localOptions.AutoRecordBossKills)
    
    ConfigureBrownieHelperRaidStartTime:SetCursorPosition(0)
    ConfigureBrownieHelperTimeAdjustment:SetCursorPosition(0)
    ConfigureBrownieHelperBiddingInstructions:SetCursorPosition(0)
    ConfigureBrownieHelperBiddingInstructions:ClearFocus()
    ConfigureBrownieHelperRaidStartTime:ClearFocus()
    ConfigureBrownieHelperTimeAdjustment:ClearFocus()

end

function ConfigureBrownieHelper:commitState()

    --Commiting anything on any of these panels could change our apparent database state; yeah it'd be nice if we made this more granular
    self.app:TriggerEvent("BrownieHelper_PointsChanged")

    self.app:getDb().Options = self.localOptions
    self:clearState()
end

function ConfigureBrownieHelper:clearState()

    local options = self.app:getDb().Options

    self.localOptions = {
        CloseAuctionDelay = options.CloseAuctionDelay,
        BiddingInstructions = options.BiddingInstructions,
        ShowBidWindow = options.ShowBidWindow,
        ShowItemWindow = options.ShowItemWindow,
        DefaultRaidStartTime = options.DefaultRaidStartTime,
        TimeAdjustmentPeriod = options.TimeAdjustmentPeriod,
        AutoRecordTimeAdjustment = options.AutoRecordTimeAdjustment,
        AutoRecordRaidStart = options.AutoRecordRaidStart,
        AutoRecordBossKills = options.AutoRecordBossKills,
    }
    
    self:updateView()
    
end

function ConfigureBrownieHelper:OnStartTime()
    self.localOptions.DefaultRaidStartTime = ConfigureBrownieHelperRaidStartTime:GetText()
end
function ConfigureBrownieHelper:OnTimeAdjustment()
    self.localOptions.TimeAdjustmentPeriod = ConfigureBrownieHelperTimeAdjustment:GetText()
end
function ConfigureBrownieHelper:OnInstructions()
    self.localOptions.BiddingInstructions = ConfigureBrownieHelperBiddingInstructions:GetText()
end
function ConfigureBrownieHelper:OnAuctionDelay()
    self.localOptions.CloseAuctionDelay = ConfigureBrownieHelperCloseAuctionDelay:GetValue()
end
function ConfigureBrownieHelper:OnShowBidWindow()
    self.localOptions.ShowBidWindow = ConfigureBrownieHelperShowBidWindow:GetChecked()
end
function ConfigureBrownieHelper:OnShowItemWindow()
    self.localOptions.ShowItemWindow = ConfigureBrownieHelperShowItemWindow:GetChecked()
end
function ConfigureBrownieHelper:OnRecordStartTime()
    self.localOptions.AutoRecordRaidStart = ConfigureBrownieHelperRecordRaidStart:GetChecked()
end
function ConfigureBrownieHelper:OnRecordTimeAdjustment()
    self.localOptions.AutoRecordTimeAdjustment = ConfigureBrownieHelperRecordTimeAdjustment:GetChecked()
end
function ConfigureBrownieHelper:OnRecordBossKills()
    self.localOptions.AutoRecordBossKills = ConfigureBrownieHelperRecordBossEvents:GetChecked()
end

--PointPool configuration panel
function ConfigurePointPools:Initialize(app)

    --save the app
    self.app = app
    
    --Setup our panelness, and inject us into the BH config section
    self.name = "Point pools"
    self.parent = "ConfigureBrownieHelper"
    self.okay = function() self:commitState() end
    self.cancel = function() self:clearState() end
    InterfaceOptions_AddCategory(self)

    --Setup the table
    ConfigurePointPoolsTable:setFields({"Pool name"})
    ConfigurePointPoolsTable.candeselect = false

    --Clearing the state will setup the structures we need
    self:clearState()

end

function ConfigurePointPools:updateView()
    
    --Clear the table
    ConfigurePointPoolsTable:clearRows()
    
    --Update the data
    for poolName,pointsDb in pairs(self.pendingPointsDb) do
        ConfigurePointPoolsTable:addRow({["Pool name"]=poolName,name=poolName})
    end
    
    --By default disable the update button b/c no pool will be selected
    ConfigurePointPoolsParseButton:Disable()
    
end

function ConfigurePointPools:clearState()
    
    --Setup an internal pointDb structure which we will possibly commit (if the user hits ok) (make it copy of the current db)
    self.pendingPointsDb = {}
    for poolName,pointsDb in pairs(self.app:getDb().pointsDb) do
        self.pendingPointsDb[poolName] = {}
        for player, points in pairs(pointsDb) do
            self.pendingPointsDb[poolName][player] = points
        end
    end
    
    --Update the table
    self:updateView()
    
end

function ConfigurePointPools:commitState()
    
    --Commit the internal structure
    self.app:getDb().pointsDb = self.pendingPointsDb
    self.app.getDb().lastInternalUpdate = getFormatedGameDateTime()
    self.app:TriggerEvent("BrownieHelper_PointsChanged")
    
    --Setup a new internal copy of the points db
    self:clearState()

end

function ConfigurePointPools:OnAdd()

    inputDialog(
        "Enter the name for the new point pool (this is case sensitive):",
        function(response)
            self.pendingPointsDb[response] = {}
            self.app:TriggerEvent("BrownieHelperConfig_PointPoolsChanged", self.pendingPointsDb)
            self:updateView()
        end
    )

end

function ConfigurePointPools:OnRemove()

    local selection = ConfigurePointPoolsTable:getSelections()[1]
    if selection then
        yesnoDialog(
            "Are you sure you want to delete the '"..selection.name.."' point pool?  All point information associated with this pool will be discarded.",
            function(response)
                if response then
                    self.pendingPointsDb[selection.name] = nil
                    self.app:TriggerEvent("BrownieHelperConfig_PointPoolsChanged", self.pendingPointsDb)
                    self:updateView()
                end
            end
        )
    end
    
end

function ConfigurePointPools:OnUpdate()
    --This could easily be updated to work for a CVS or other delimiter
    
    --Capture the current points db has in this closure
    local currentPointPool = ConfigurePointPoolsTable:getSelections()[1].name
    
    --Get each line the text block
    local lines = valueSplit(ConfigurePointPoolsEditBox:GetText(), "\n")
    
    if not lines then
        messageDialog("Unable to parse the entered text.  Make sure rows are separated by newline characters.")
    else
        
        --Setup a temporary structure to hold the parsed results
        local newPointsDb = {}
        local firstLine = ""
        local lastLine = ""
        
        --For each line
        for i,line in pairs(lines) do
            
            --Split the values on tabs \t
            local values = valueSplit(line, "    ")
            
            --Update the points table, assume value #1 is name and value #n is points
            newPointsDb[values[1]] = values[#values]
            
            if i==1 then firstLine = values[1]..": "..values[#values]
            elseif i==#lines then lastLine = values[1]..": "..values[#values]
            end
            
        end
        
        yesnoDialog(
            "The entered text has been parsed.\nRows: "..tostring(#lines).."\nFirst row: "..firstLine.."\nLast row: "..lastLine.."\n\nIf that looks right, hit Ok to commit the uploaded table to the database.",
            function(response)
                if response then
                    self.pendingPointsDb[currentPointPool] = newPointsDb
                end
            end
        )
        
    end

    -- Clear out the text box
    ConfigurePointPools:OnClear()

end

function ConfigurePointPools:OnClear()
    ConfigurePointPoolsEditBox:SetText("")
    ConfigurePointPoolsParseButton:Disable()
end

function ConfigurePointPools:OnText()
    if ConfigurePointPoolsTable:getSelections()[1] then ConfigurePointPoolsParseButton:Enable() end 
end

--ConfigureMinimumBids panel
function ConfigureMinimumBids:Initialize(app)

    --save the app
    self.app = app
    
    --Setup our panelness, and inject us into the BH config section
    self.name = "Minimum bids"
    self.parent = "ConfigureBrownieHelper"
    self.okay = function() self:commitState() end
    self.cancel = function() self:clearState() end
    InterfaceOptions_AddCategory(self)

    --Setup the table
    ConfigureMinimumBidsTable:setFields({"Pool name","Minimum Bid"})
    ConfigureMinimumBidsTable.candeselect = false
    ConfigureMinimumBidsTable:registerDoubleClickCallback(function(row) self:OnEdit() end)

    --Update our options when the pending point pools change
    self.app:registerEvent("BrownieHelperConfig_PointPoolsChanged", function() self:clearState() end)

    --Clearing the state will setup the structures we need
    self:clearState()
    
end

function ConfigureMinimumBids:updateView()

    ConfigureMinimumBidsTable:clearRows()
    for poolName, bid in pairs(self.pendingMinimumBids) do
        ConfigureMinimumBidsTable:addRow({
            ["Pool name"] = poolName,
            ["Minimum Bid"] = tostring(bid),
        })
    end
    
end

function ConfigureMinimumBids:commitState()

    self.app:getDb().MinimumBids = self.pendingMinimumBids
    self:clearState()

end

function ConfigureMinimumBids:clearState()

    --Setup an internal pointDb structure which we will possibly commit (if the user hits ok) (make it copy of the current db)
    self.pendingMinimumBids = {}
    for poolName,bid in pairs(self.app:getDb().MinimumBids) do
        self.pendingMinimumBids[poolName] = bid
    end
    
    --Make an entry for each point pool that isn't in the table
    for poolName, pointsDb in pairs(ConfigurePointPools.pendingPointsDb) do
        if self.pendingMinimumBids[poolName]==nil then self.pendingMinimumBids[poolName] = 0 end
    end
    
    --Update the table
    self:updateView()
    
end

function ConfigureMinimumBids:OnEdit()

    local selection = ConfigureMinimumBidsTable:getSelections()[1]["Pool name"]
    if selection then
        inputDialog(
            "Enter the new minimum bid for the "..selection.." point pool:",
            function(response)
                self.pendingMinimumBids[selection] = response
                self:updateView()
            end
        )
    end

end

--Events configuration panel
function ConfigureEvents:Initialize(app)

    --save the app
    self.app = app
    
    --Setup our panelness, and inject us into the BH config section
    self.name = "Events"
    self.parent = "ConfigureBrownieHelper"
    self.okay = function() self:commitState() end
    self.cancel = function() self:clearState() end
    InterfaceOptions_AddCategory(self)

    --Setup the tables
    ConfigureEventsPoolsTable:setFields({"Point pool"})
    ConfigureEventsPoolsTable.candeselect = false
    ConfigureEventsPoolsTable:registerSelectionCallback(function() self:updateEventsView() end)
    
    ConfigureEventsEventsTable:setFields({"Event name"})
    ConfigureEventsEventsTable.candeselect = false

    --Update our options when the pending point pools change
    self.app:registerEvent("BrownieHelperConfig_PointPoolsChanged", function() self:clearState() end)
    
    --Clearing the state will setup the structures we need
    self:clearState()

end

function ConfigureEvents:updatePoolsView()

    ConfigureEventsPoolsTable:clearRows()
    for poolName, events in pairs(self.pendingEventMap) do
        ConfigureEventsPoolsTable:addRow({
            ["Point pool"] = poolName,
        })
    end
    
    self:updateEventsView()

end

function ConfigureEvents:updateEventsView()

    local selection = ConfigureEventsPoolsTable:getSelections()[1]
    
    ConfigureEventsEventsTable:clearRows()
    if selection then
        for i, eventName in pairs(self.pendingEventMap[selection["Point pool"]]) do
            ConfigureEventsEventsTable:addRow({
                ["Event name"] = eventName,
            })
        end
    end

end

function ConfigureEvents:commitState()

    self.app:getDb().Events = self.pendingEventMap
    self:clearState()

end

function ConfigureEvents:clearState()

    self.pendingEventMap = {}
    for poolName, events in pairs(self.app:getDb().Events) do
        self.pendingEventMap[poolName] = {}
        for i,eventName in pairs(events) do
            table.insert(self.pendingEventMap[poolName], eventName)
        end
    end
    
    --Make an entry for each point pool that isn't in the table
    for poolName, pointsDb in pairs(ConfigurePointPools.pendingPointsDb) do
        if self.pendingEventMap[poolName]==nil then self.pendingEventMap[poolName] = {} end
    end
    
    self:updatePoolsView()

end

function ConfigureEvents:OnAdd()
    
    local selection = ConfigureEventsPoolsTable:getSelections()[1]
    
    if selection then
        inputDialog(
            "Enter the name of the new event:",
            function(response)
                table.insert(self.pendingEventMap[selection["Point pool"]], response)
                self.app:TriggerEvent("BrownieHelperConfig_EventsChanged")
                self:updateEventsView()
            end
        )
    end
    
end

function ConfigureEvents:OnRemove()

    local poolSelection = ConfigureEventsPoolsTable:getSelections()[1]
    local eventSelection = ConfigureEventsEventsTable:getSelections()[1]
    
    if poolSelection and eventSelection then
        tableRemove(self.pendingEventMap[poolSelection["Point pool"]], eventSelection["Event name"])
        self.app:TriggerEvent("BrownieHelperConfig_EventsChanged")
        self:updateEventsView()
    end

end

--EventRewards panel
function ConfigureEventRewards:Initialize(app)

    --save the app
    self.app = app
    
    --Setup our panelness, and inject us into the BH config section
    self.name = "Event rewards"
    self.parent = "ConfigureBrownieHelper"
    self.okay = function() self:commitState() end
    self.cancel = function() self:clearState() end
    InterfaceOptions_AddCategory(self)

    --Setup the table
    ConfigureEventRewardsTable:setFields({"Event","Reward"})
    ConfigureEventRewardsTable.candeselect = false
    ConfigureEventRewardsTable:registerDoubleClickCallback(function() self:OnEdit() end)

    --Watch for changes in the event list
    --self.app:registerEvent("BrownieHelperConfig_EventsChanged", function() self:clearState() end)

    --Clearing the state will setup the structures we need
    self:clearState()
    
end

function ConfigureEventRewards:updateView()

    ConfigureEventRewardsTable:clearRows()
    for eventName, points in pairs(self.pendingEventRewards) do
        ConfigureEventRewardsTable:addRow({
            ["Event"] = eventName,
            ["Reward"] = points,
        }
        )
    end
    
end

function ConfigureEventRewards:commitState()

    self.app:getDb().EventRewards = self.pendingEventRewards
    self:clearState()

end

function ConfigureEventRewards:clearState()

    self.pendingEventRewards = {}
    for eventName, points in pairs(self.app:getDb().EventRewards) do
        self.pendingEventRewards[eventName] = points
    end
    
    --Make an entry for each event, if one isn't found
    for poolName,events in pairs(ConfigureEvents.pendingEventMap) do
        for i,eventName in pairs(events) do
            if self.pendingEventRewards[eventName]==nil then self.pendingEventRewards[eventName]=0 end
        end
    end
    
    ConfigureEventRewards:updateView()

end

function ConfigureEventRewards:OnEdit()
    
    local selection = ConfigureEventRewardsTable:getSelections()[1]
    
    if selection then
        inputDialog(
            "Enter the new point reward for "..selection.Event,
            function(response)
                self.pendingEventRewards[selection.Event] = response
                self:updateView()
            end
        )
    end
    
end


--LootTypes panel
function ConfigureLootCategories:Initialize(app)

    --save the app
    self.app = app
    
    --Setup our panelness, and inject us into the BH config section
    self.name = "Auctions"
    self.parent = "ConfigureBrownieHelper"
    self.okay = function() self:commitState() end
    self.cancel = function() self:clearState() end
    InterfaceOptions_AddCategory(self)

    --Setup the table
    ConfigureLootCategoriesTypesTable:setFields({"Bidding system"})
    ConfigureLootCategoriesTypesTable.candeselect = false
    ConfigureLootCategoriesTypesTable:registerSelectionCallback(function() self:updateItemsView() end)
    
    ConfigureLootCategoriesItemsTable:setFields({"Items"})
    ConfigureLootCategoriesItemsTable.candeselect = false
    ConfigureLootCategoriesItemsTable:registerRowEnterCallback(function(row) self:rowEnter(row) end)
    ConfigureLootCategoriesItemsTable:registerRowLeaveCallback(function(row) self:rowLeave(row) end)
    

    --Clearing the state will setup the structures we need
    self:clearState()
    
end

function ConfigureLootCategories:updateTypesView()

    ConfigureLootCategoriesTypesTable:clearRows()
    for systemName, system in pairs(BiddingSystems) do
        ConfigureLootCategoriesTypesTable:addRow({
            ["Bidding system"] = systemName,
        })
    end

    self:updateItemsView()
    
end

function ConfigureLootCategories:updateItemsView()

    local selection = ConfigureLootCategoriesTypesTable:getSelections()[1]
    
    ConfigureLootCategoriesItemsTable:clearRows()
    if selection then
        local items = self.pendingLootTypes[selection["Bidding system"]]
        if items then
            for i,item in pairs(items) do
                ConfigureLootCategoriesItemsTable:addRow({
                    ["Items"] = item,
                })
            end
        end
    end

end

function ConfigureLootCategories:updateDefaultView()
    ConfigureLootCategoriesSubText2:SetText(valueJoin(fromValues(self.pendingDefaultSequence,'name'),", "))
end

function ConfigureLootCategories:commitState()

    self.app:getDb().LootTypes = self.pendingLootTypes
    
    local newSequence = {}
    for i, system in pairs(self.pendingDefaultSequence) do table.insert(newSequence, system.name) end
    self.app:getDb().DefaultBiddingSequence = newSequence
    
    self:clearState()

end

function ConfigureLootCategories:clearState()

    self.pendingLootTypes = {}
    for lootType, items in pairs(self.app:getDb().LootTypes) do
        self.pendingLootTypes[lootType] = {}
        for i,item in pairs(items) do
            table.insert(self.pendingLootTypes[lootType], item)
        end
    end

    self.pendingDefaultSequence = {}
    for i,k in pairs(self.app:getDb().DefaultBiddingSequence) do
        self.pendingDefaultSequence[i]=BiddingSystems[k]
    end

    self:updateTypesView()
    self:updateDefaultView()

end

function ConfigureLootCategories:OnItemAdd()

    local selection = ConfigureLootCategoriesTypesTable:getSelections()[1]
    
    if selection then
        inputDialog(
            "Enter the name of a new item to associate with the selected bidding system:",
            function(response)
                if not self.pendingLootTypes[selection["Bidding system"]] then self.pendingLootTypes[selection["Bidding system"]]={} end
                table.insert(self.pendingLootTypes[selection["Bidding system"]], response)
                self:updateItemsView()
            end
        )
    end

end

function ConfigureLootCategories:OnItemRemove()

    local typeSelection = ConfigureLootCategoriesTypesTable:getSelections()[1]
    local itemSelection = ConfigureLootCategoriesItemsTable:getSelections()[1]
    
    if itemSelection then
        tableRemove(self.pendingLootTypes[typeSelection["Bidding system"]], itemSelection.Items)
        self:updateItemsView()
    end
    
end

function ConfigureLootCategories:OnDefault()
    --Kind of a cheap little fake out, but easy!
    
    EditBiddingSystemFrame:editBiddingSystems(
        {biddingsystems=self.pendingDefaultSequence},
        function()
            self:updateDefaultView()
        end
    )
    
end

function ConfigureLootCategories:rowEnter(row)

    local itemName, itemLink = GetItemInfo(row.Items) 

    if itemLink then
        --position & feed the game tool tip
        GameTooltip_SetDefaultAnchor( GameTooltip, ConfigureLootCategories )
        GameTooltip:ClearAllPoints()
        GameTooltip:SetPoint("TOPLEFT", ConfigureLootCategories, "TOPRIGHT", 0, -15)
        GameTooltip:SetHyperlink(itemLink)    
        GameTooltip:Show()
    end
    
end

function ConfigureLootCategories:rowLeave(row)
    GameTooltip:Hide()
end

--Permissions panel
function ConfigurePermissions:Initialize(app)

    --save the app
    self.app = app
    
    --Setup our panelness, and inject us into the BH config section
    self.name = "Permissions"
    self.parent = "ConfigureBrownieHelper"
    self.okay = function() self:commitState() end
    self.cancel = function() self:clearState() end
    InterfaceOptions_AddCategory(self)

    --Setup the table
    ConfigurePermissionsPermissionsTable:setFields({"Permission"})
    ConfigurePermissionsPermissionsTable.candeselect = false
    ConfigurePermissionsPermissionsTable:registerSelectionCallback(function() self:updatePlayersView() end)
    
    ConfigurePermissionsPlayersTable:setFields({"Players"})
    ConfigurePermissionsPlayersTable.candeselect = false
    

    --Clearing the state will setup the structures we need
    self:clearState()
    
end

function ConfigurePermissions:updatePermissionsView()

    ConfigurePermissionsPermissionsTable:clearRows()
    for lootPermission, players in pairs(self.pendingPermissions) do
        ConfigurePermissionsPermissionsTable:addRow({
            ["Permission"] = lootPermission,
        })
    end

    self:updatePlayersView()
    
end

function ConfigurePermissions:updatePlayersView()

    local selection = ConfigurePermissionsPermissionsTable:getSelections()[1]
    
    ConfigurePermissionsPlayersTable:clearRows()
    if selection then
        for i,player in pairs(self.pendingPermissions[selection["Permission"]]) do
            ConfigurePermissionsPlayersTable:addRow({
                ["Players"] = player,
            })
        end
    end

end

function ConfigurePermissions:commitState()

    self.app:getDb().Permissions = self.pendingPermissions
    self:clearState()

end

function ConfigurePermissions:clearState()

    self.pendingPermissions = {}
    for lootPermission, players in pairs(self.app:getDb().Permissions) do
        self.pendingPermissions[lootPermission] = {}
        for i,player in pairs(players) do
            table.insert(self.pendingPermissions[lootPermission], player)
        end
    end

    self:updatePermissionsView()

end

function ConfigurePermissions:OnPermissionAdd()

    inputDialog(
        "Enter a name for the new loot permission:",
        function(response)
            self.pendingPermissions[response] = {}
            self:updatePermissionsView()
        end
    )

end

function ConfigurePermissions:OnPermissionRemove()

    local selection = ConfigurePermissionsPermissionsTable:getSelections()[1]
    
    if selection then
        self.pendingPermissions[selection["Permission"]] = nil
        self:updatePermissionsView()
    end

end

function ConfigurePermissions:OnPlayerAdd()

    local selection = ConfigurePermissionsPermissionsTable:getSelections()[1]
    
    if selection then
        inputDialog(
            "Enter the name of a new player:",
            function(response)
                table.insert(self.pendingPermissions[selection["Permission"]], response)
                self:updatePlayersView()
            end
        )
    end

end

function ConfigurePermissions:OnPlayerRemove()

    local permissionSelection = ConfigurePermissionsPermissionsTable:getSelections()[1]
    local playerSelection = ConfigurePermissionsPlayersTable:getSelections()[1]
    
    if playerSelection then
        tableRemove(self.pendingPermissions[permissionSelection["Permission"]], playerSelection.Players)
        self:updatePlayersView()
    end
    
end

